<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!--引入富文本编辑器相关样式文件-->
    <link href="https://cdn.staticfile.org/wangeditor5/5.1.23/css/style.min.css" rel="stylesheet">
    <!--引入Axios-->
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>

</head>
<body>
<div id="app">
    <el-container>
        <el-header height="70px" style="background-color: #386582">
            <h1 style="color: white;font-size: 30px;margin: 0;
                            line-height: 70px">
                烘焙坊后台管理系统
                <span style="font-size: 20px;float: right">欢迎{{user.nickName}}回来!
                    <a href="javascript:void(0)" @click="logout()"
                       style="color: orange">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu @select="handleSelect" default-active="1" style="height: 1000px" active-text-color="orange">
                      <img src="imgs/icon.png" width="150px">
                    <el-divider></el-divider>
                    <el-menu-item index="1">
                        <template slot="title">
                            <i class="el-icon-user"></i><span>用户管理</span>
                        </template>
                    </el-menu-item>
                    <el-menu-item index="2">
                        <template slot="title">
                            <i class="el-icon-picture"></i><span>轮播图</span>
                        </template>
                    </el-menu-item>
                    <el-menu-item index="3">
                        <template slot="title">
                            <i class="el-icon-s-order"></i><span>烘焙食谱</span>
                        </template>
                    </el-menu-item>
                    <el-menu-item index="4">
                        <template slot="title">
                            <i class="el-icon-video-camera"></i><span>烘焙视频</span>
                        </template>
                    </el-menu-item>
                    <el-menu-item index="5">
                        <template slot="title">
                            <i class="el-icon-info"></i><span>行业资讯</span>
                        </template>
                    </el-menu-item>
                </el-menu>

            </el-aside>
            <el-main>
                <!--用户表格开始-->
                <el-table v-if="currentIndex==1" :data="userArr">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column prop="userName" label="用户名"></el-table-column>
                    <el-table-column prop="nickName" label="昵称"></el-table-column>
                    <el-table-column label="头像">
                        <template slot-scope="scope">
                             <el-avatar :src="scope.row.imgUrl"></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column label="管理员">
                        <template slot-scope="scope">
                            <el-switch @change="changeAdmin(scope.row)" v-model="scope.row.isAdmin"></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="danger"
                                       @click="deleteUser(scope.$index,scope.row)">删除</el-button>
                        </template>
                    </el-table-column>

                </el-table>
                <!--用户表格结束-->
                <!--轮播图表格开始-->
                <el-table v-if="currentIndex==2" :data="bannerArr">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column label="轮播图">
                        <template slot-scope="scope">
                            <img :src="scope.row.imgUrl" width="200px">
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="danger"
                                       @click="deleteBanner(scope.$index,scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--轮播图表格结束-->
                <!--内容表格开始-->
                <el-table v-if="currentIndex>2" :data="contentArr" border>
                    <el-table-column label="编号" width="50px" type="index"></el-table-column>
                    <el-table-column label="标题" width="150px"
                                     prop="title" align="center"></el-table-column>
                    <el-table-column label="封面" width="60px">
                        <template slot-scope="scope">
                            <img :src="scope.row.imgUrl" width="50px">
                        </template>

                    </el-table-column>
                    <el-table-column v-if="currentIndex!=4" label="摘要" width="300px"
                                     prop="brief" align="center"></el-table-column>
                    <el-table-column label="类型"
                                     prop="categoryName" align="center"></el-table-column>
                    <el-table-column label="浏览量"
                                     prop="viewCount" align="center"></el-table-column>
                    <el-table-column label="回复量"
                                     prop="commentCount" align="center"></el-table-column>
                    <el-table-column label="创建时间"
                                     prop="createTime" align="center"></el-table-column>
                    <el-table-column label="操作"  width="200px" fixed="right"
                                     align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger"
                                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--内容表格结束-->
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                user:JSON.parse(localStorage.user),
                userArr:[],
                bannerArr:[],
                contentArr:[],
                currentIndex:"1"
            }
        },
        methods: {
            handleDelete(i,content){
              if (confirm("您确定删除此内容吗?")){
                  axios.post("/v1/contents/"+content.id+"/delete").then(function (response) {
                      if (response.data.code==1){
                          v.$message.success("删除完成!");
                          v.contentArr.splice(i,1);
                      }
                  })
              }
            },
            handleEdit(i,content){
                location.href="/postArticle.html?id="+content.id;
            },
            deleteBanner(i,banner){
                if (confirm("您确认删除此轮播图吗？")){
                    axios.post("/v1/banners/"+banner.id+"/delete").then(function (response) {
                        if (response.data.code==1){
                            v.bannerArr.splice(i,1);//删除数组中的数据
                        }
                    })
                }
            },
            deleteUser(i,user){
                if (user.id==v.user.id){
                    alert("不能删除当前登录的用户!");
                    return;
                }
              if (confirm("您确认删除此用户吗？")){
                  axios.post("/v1/users/"+user.id+"/delete").then(function (response) {
                      if (response.data.code==1){
                          v.userArr.splice(i,1);//删除数组中的数据
                      }
                  })
              }
            },
            changeAdmin(user){
                axios.post("/v1/users/"+user.id+"/"+(user.isAdmin?1:0)+"/change")
                    .then(function (response) {
                    if (response.data.code==1){
                        v.$message.success("修改完成！");
                    }
                })
            },
            handleSelect(key,keyPath){
                //key就是点击菜单项的index值
                v.currentIndex = key;
                if (key>=3){
                    let type= key-2;
                    //请求和类型相对于的从容
                    axios.get("/v1/contents/"+type+"/admin").then(function (response) {
                        if (response.data.code==1){
                            v.contentArr = response.data.data;
                        }
                    })
                }

            },
            logout(){
                if (confirm("您确认退出登录吗?")){
                    axios.get("/v1/users/logout").then(function () {
                        localStorage.clear();
                        location.href="/";
                    })
                }

            }
        },
        created:function () {
            let user = JSON.parse(localStorage.user);
            if (user.isAdmin!=1){
                alert("请登录管理员账户!");
                location.href="/login.html";
            }
            //请求用户列表数据
            axios.get("/v1/users/").then(function (response) {
                if (response.data.code==1){
                    v.userArr = response.data.data;
                }
            })
            //请求所有轮播图数据
            axios.get("/v1/banners/admin").then(function (response) {
                if (response.data.code==1){
                    v.bannerArr = response.data.data;
                }
            })
        }
    })
</script>
</html>